using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using Musicman.Business;
using MusicmanWeb.Custom;

namespace MusicmanWeb
{


    public partial class FilesOverview : MusicmanPage
    {

        protected override void Page_Load(object sender, EventArgs e)
        {
            ReddirectNonActiveSession();
        }

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            // determine which one is visible

            MyGridView gv = null;
            if (this.SearchGV.Visible)
            {
                gv = this.SearchGV;
            }
            else if (TopDownloadsGV.Visible)
            {
                gv = TopDownloadsGV;
            }
            else if (RandomGV.Visible)
            {
                gv = RandomGV;
            }

            if (gv != null)
            {
                List<long> fileIds = new List<long>();
                foreach (int index in gv.GetSelectedIndices())
                {
                    long key = (long)gv.DataKeys[index].Value;
                    fileIds.Add(key);
                }
                long userid = Int64.Parse(Session["UserID"].ToString());
                DataManager.Instance.AddPendingDownloadsList(userid, fileIds);
                gv.DataBind();
            }
        }

        protected void Button2_Click(object sender, CommandEventArgs e)
        {
            this.UpdateButton.Visible = true;
            switch (e.CommandName)
            {
                case "Search":
                    this.SearchGV.DataBind();
                    this.SearchGV.PageIndex = 0;
                    this.SearchGV.Visible = true;
                    this.TopDownloadsGV.Visible = false;
                    this.TopRatedGV.Visible = false;
                    this.RandomGV.Visible = false;
                    this.LatestGV.Visible = false;
                    break;

                case "Dowlnoads":
                    this.SearchGV.Visible = false;
                    this.TopDownloadsGV.DataBind();
                    this.TopDownloadsGV.PageIndex = 0;
                    this.TopDownloadsGV.Visible = true;
                    this.TopRatedGV.Visible = false;
                    this.RandomGV.Visible = false;
                    this.LatestGV.Visible = false;
                    break;

                case "Rating":
                    this.SearchGV.Visible = false;
                    this.TopDownloadsGV.Visible = false;
                    this.TopRatedGV.DataBind();
                    this.TopRatedGV.PageIndex = 0;
                    this.TopRatedGV.Visible = true;
                    this.RandomGV.Visible = false;
                    this.LatestGV.Visible = false;
                    break;

                case "Random":
                    this.SearchGV.Visible = false;
                    this.TopDownloadsGV.Visible = false;
                    this.TopRatedGV.Visible = false;
                    this.RandomGV.DataBind();
                    this.RandomGV.PageIndex = 0;
                    this.RandomGV.Visible = true;
                    this.LatestGV.Visible = false;
                    break;

                case "Latest":
                    this.SearchGV.Visible = false;
                    this.TopDownloadsGV.Visible = false;
                    this.TopRatedGV.Visible = false;
                    this.RandomGV.Visible = false;
                    this.LatestGV.DataBind();
                    this.LatestGV.PageIndex = 0;
                    this.LatestGV.Visible = true;
                    break;

                default :
                    this.SearchGV.Visible = false;
                    this.TopDownloadsGV.Visible = false;
                    this.TopRatedGV.Visible = false;
                    this.RandomGV.Visible = false;
                    this.UpdateButton.Visible = false;
                    this.LatestGV.Visible = false;
                    break;
            } 
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //this.GridView3.Visible = false;
            //this.GridView2.Visible = true;
        }

        protected void GridView_DataBound(object sender, EventArgs e)
        {
            MyGridView gv = sender as MyGridView;
            if (gv.Rows.Count != 0)
            {
                GridViewRow pagerRow = gv.BottomPagerRow;
                Label current = (Label)pagerRow.FindControl("currentPage");
                current.Text = String.Format("{0}", gv.PageIndex + 1);
                Label total = (Label)pagerRow.FindControl("totalPages");
                total.Text = String.Format("{0}", gv.PageCount);

                int firstPage = (gv.PageIndex / 5) * 5;
                int lastPage = (firstPage + 5 > gv.PageCount) ? gv.PageCount : firstPage + 5;

                PlaceHolder pages = (PlaceHolder)pagerRow.FindControl("pages");
                for (int i = 1; i <= 5; i++)
                {
                    ((LinkButton)pages.FindControl("Page" + i)).Visible = false;
                }

                for (int i = firstPage; i < lastPage; i++)
                {
                    int index = i % 5 + 1;
                    LinkButton lb = (LinkButton)pages.FindControl("Page" + index);
                    lb.Visible = true;
                    lb.Text = String.Format("{0}", i + 1);
                    lb.CommandArgument = String.Format("{0}", i + 1);
                }
            }
            else
            {
                this.UpdateButton.Visible = false;
            }
        }
    }
}
